﻿#region Using Directives

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Resources;
    using System.Threading;
    using System.Web;
    using System.Net.Mail;   

    using MarketPulse.Web.Helpers;

#endregion

namespace MarketPulse.Web.UI
{

    /// <summary>
    /// Summary description for Page
    /// </summary>
    public abstract class Page : System.Web.UI.Page
    {
        #region Member Variables

            /// <summary>Local variable used to determine whether a page should check if a user is logged on before continuing</summary>
            protected bool mCheckAuthentication = true;

            /// <summary>Property that stores the name of the class.</summary>
            protected static readonly Type mClass = System.Reflection.MethodBase.GetCurrentMethod().DeclaringType;

            /// <summary>Property that stores the log4net logger for this class.</summary>
            protected static readonly log4net.ILog mLog = log4net.LogManager.GetLogger(mClass);
            
        #endregion

        #region Methods

            #region Handlers

                /// <summary>
                /// Method used to handle the preinit event for this page
                /// </summary>
                /// <param name="e">The event arguments passed by the object that raised this event</param>
                protected override void OnPreInit(EventArgs e)
                {
                    base.OnPreInit(e);

                    if (mCheckAuthentication)
                    {
                        if (SessionManager.Authenticated == false)
                        {
                            SessionManager.LastUrl = Request.Url.PathAndQuery;
                            Response.Redirect(Page.ResolveUrl("~/Login.aspx"));
                        }
                    }
                }

                /// <summary>
                /// Method used to handle the init event for this page
                /// </summary>
                /// <param name="e">The event arguments passed by the object that raised this event</param>
                protected override void OnInit(EventArgs e)
                {
                    base.OnInit(e);
                }

                /// <summary>
                /// Method used to handle the load event for this page
                /// </summary>
                /// <param name="e">The event arguments passed by the object that raised this event</param>
                protected override void OnLoad(EventArgs e)
                {
                    base.OnLoad(e);

                    InitializeCulture();
                }

            #endregion

            /// <summary>
            /// (Overriden) Method used to initialize the culture for the Page
            /// </summary>               
            protected override void InitializeCulture()
            {
                var language = CookieManager.Language;
                if (!String.IsNullOrEmpty(language))
                {
                    UICulture = language;
                    Culture = language;

                    Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.CreateSpecificCulture(language);
                    Thread.CurrentThread.CurrentUICulture = new System.Globalization.CultureInfo(language);

                    base.InitializeCulture();
                }
            }

            /// <summary>
            /// Method to send Mail
            /// </summary>
            /// <param name="fromAddress">From address from which mail address is intended to send </param>
            /// <param name="toAddress">To address to which mail address is intended to send </param>
            /// <param name="subject">Subject of the mail which is intended to send </param>
            /// <param name="message">body of the mail which is intended to send </param>
            /// <param name="isHTML">Parameter to check whether mail need to sent in HTML format or not</param>
            public void SendMail(string fromAddress, string toAddress, string subject, string message, bool isHTML)
            {
                try
                {
                    MailMessage mailmsg = new MailMessage();

                    mailmsg.From = new MailAddress(fromAddress); //from address
                    mailmsg.To.Add(toAddress); //to address
                    mailmsg.Subject = subject; //Subject
                    mailmsg.IsBodyHtml = isHTML; //from argument
                    mailmsg.Body = message; //Message boby

                    SmtpClient smtpserver = new SmtpClient("127.0.0.1");
                    //smtp.Credentials = new System.Net.NetworkCredential(“YourSMTPServerUserName”, “YourSMTPServerPassword”); 
                    
                    smtpserver.Send(mailmsg);

                    mailmsg.Dispose();
                }
                catch (Exception ex)
                {
                    string exception = ex.InnerException.Message.ToString();
                }
            }

        #endregion

    }
}